package com.jbit.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiRenZhi
 * @description 公共结果返回
 */
@Data
public class RespBean {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    /**
     * 构造方法私有化,里面的方法都是静态方法
     * 达到保护属性的作用
     */
    private RespBean() {
    }

    private RespBean(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    /**
     * 这里是使用链式编程
     */
    public static RespBean success() {
        return new RespBean(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static RespBean error() {
        return new RespBean(false, ResultCode.COMMON_FAIL.getCode(), ResultCode.COMMON_FAIL.getMessage());
    }

    public static RespBean error(ResultCode resultCode) {
        return new RespBean(false, resultCode.getCode(), resultCode.getMessage());
    }

    /**
     * 自定义返回成功与否
     *
     * @param success
     * @return
     */
    public RespBean success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public RespBean message(String message) {
        this.setMessage(message);
        return this;
    }

    public RespBean code(Integer code) {
        this.setCode(code);
        return this;
    }

    public RespBean data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public RespBean data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}